﻿Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration

' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)


Partial Public Class VisualWebPart1UserControl
    Inherits UserControl
    Dim farm As SPFarm = SPFarm.Local

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If (Page.IsPostBack = False) Then
            Dim service As SPWebService = farm.Services.GetValue(Of SPWebService)()
            For Each webapp As SPWebApplication In service.WebApplications
                For Each siteCollection As SPSite In webapp.Sites
                    Dim url As String = siteCollection.Url
                    uxSites.Items.Add(url)
                Next
            Next
            uxSites.Items(0).Selected = True
            GetAllListData(uxSites.SelectedItem.ToString())
        End If

    End Sub

    Protected Sub uxSites_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles uxSites.SelectedIndexChanged
        GetAllListData(uxSites.SelectedItem.ToString())
    End Sub

    Private Sub GetAllListData(ByVal selectedsite As String)
        Using siteselected As New SPSite(selectedsite)
            Dim listpropcollection As List(Of ListProperties) = New List(Of ListProperties)
            For Each web As SPWeb In siteselected.AllWebs
                For Each list As SPList In web.Lists
                    Dim listprops As New ListProperties()
                    listprops.ListTitle = list.Title
                    listprops.ItemCount = list.ItemCount
                    If Not list.Folders Is Nothing Then
                        listprops.FolderCount = list.Folders.Count
                    Else
                        listprops.FolderCount = 0
                    End If
                    If Not list.Fields Is Nothing Then
                        listprops.FieldCount = list.Fields.Count
                    Else
                        listprops.FieldCount = 0
                    End If
                    If Not list.Views Is Nothing Then
                        listprops.ViewCount = list.Views.Count
                    Else
                        listprops.ViewCount = 0
                    End If
                    If Not list.WorkflowAssociations Is Nothing Then
                        listprops.WorkflowCount = list.WorkflowAssociations.Count
                    Else
                        listprops.WorkflowCount = 0
                    End If
                    listpropcollection.Add(listprops)
                Next
                If web IsNot Nothing Then
                    web.Dispose()
                End If
            Next
            GridView1.DataSource = listpropcollection
            GridView1.DataBind()
        End Using

    End Sub
End Class
